<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="quan">quan</div>
    <script>
      // 1、赋值文本
      // dayOfYear('Hello World')
      const copyToClipboard = (text) => {
        navigator.clipboard.writeText(text);
      };

      // 2、获取第几天
      // dayOfYear(new Date(2023, 4, 2))
      const dayOfYear = (date) => {
        return Math.floor(
          (date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24
        );
      };

      // 3、快速评分组件
      // console.log(rate(0));
      // console.log(rate(1));
      // console.log(rate(2));
      // console.log(rate(5));
      const rate = (r) => "★★★★★☆☆☆☆☆".slice(5 - r, 10 - r);

      // 4、灰度算法
      // console.log(gray(50, 100, 150));
      const gray = (r, g, b) => 0.2126 * r + 0.7152 * g + 0.0722 * b;

      // 5、解析url中的参数
      // console.log(parseQuery('https://baidu.com/?a=1&b=2'))
      // console.log(parseQuery('https://baidu.com/?a=c&b=d'))
      const parseQuery = (url) => {
        const q = {};
        url.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => (q[k] = v));
        return q;
      };

      // 6、对象筛选属性
      // console.log(pick({ a: 1, b: 2, c: 3 }, "a", "b"));
      const pick = (obj, ...props) => {
        return Object.fromEntries(
          Object.entries(obj).filter(([k]) => props.includes(k))
        );
      };

      // 7、随机HEX颜色
      // console.log(randomColor());
      const randomColor = () => {
        return (
          "#" +
          Math.floor(Math.random() * 0xffffff)
            .toString(16)
            .padEnd(6, "0")
        );
      };

      // 8、随机字符串
      // console.log(randomString());
      const randomString = (len = 6) => {
        // return Math.random().toString(36).slice(2);
        if (len <= 11) {
          return Math.random()
            .toString(36)
            .slice(2, 2 + len)
            .padEnd(len, "0");
        } else {
          return randomString(11) + randomString(len - 11);
        }
      };

      // 9、去除字符串的元素标记
      // console.log(removeTag('<div>Hello World<div> QUAN</div></div>'))
      const removeTag = (fragment) => {
        return (
          new DOMParser().parseFromString(fragment, "text/html").body
            .textContent || ""
        );
      };

      // 10、cookie转换为对象
      const getCookie = () =>
        document.cookie
          .split(";")
          .map((item) => item.split("="))
          .reduce(
            (acc, [k, v]) => (acc[k.trim().replace('"', "")] = v) && acc,
            {}
          );

      // 11、打乱数组
      // console.log(randomSort([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
      const randomSort = (arr) => {
        return arr.sort(() => Math.random() - 0.5);
      };
    </script>
  </body>
</html>
